﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Linq;
using BasicDataManagement;
using DataModel;

namespace SystemWindow
{
    /// <summary>
    /// MoneyChangeWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MoneyChangeWindow : Window
    {
        IOpertation op;
        SystemUser user;
        private MoneyChangeWindow()
        {
            InitializeComponent();
            op = OperationFactory.Create();
        }
        public MoneyChangeWindow(SystemUser user)
            : this()
        {
            currJobNum.Text = user.StaffJobNum;
            currName.Text = user.StaffName;
            this.user = user;
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (op.CheckMoneyVerify(moneyJobNum.Text.Trim(), moneyPwd.Password) == false)
            {
                MessageBox.Show("无权限进行给取款操作！");
                return;
            }
            XElement xmlfile = XElement.Load("MoneyBox.xml");
            if ( giveRB.IsChecked!=true && takeRB.IsChecked!=true){
                MessageBox.Show("请选择钱款方向");
                return;
            }
            if (giveRB.IsChecked == true)
            {

                decimal source = Convert.ToDecimal(xmlfile.Element("Rest").Value);
                source+= Convert.ToDecimal(valueTB.Text);
                //插入给取记录
                op.AddMoneyRecord(user.LogID, moneyJobNum.Text.Trim(), false, Convert.ToDecimal(valueTB.Text));
                xmlfile.Element("Rest").Value = source.ToString();
                xmlfile.Save("MoneyBox.xml");
                PosPrinter.POSPrinter printer = new PosPrinter.POSPrinter();
                printer.OpenBox();
                this.Close();
                return;
            }
            if (takeRB.IsChecked == true)
            {
                decimal source = Convert.ToDecimal(xmlfile.Element("Rest").Value);
                source -= Convert.ToDecimal(valueTB.Text);
                //插入给取记录
                op.AddMoneyRecord(user.LogID, moneyJobNum.Text.Trim(), true, Convert.ToDecimal(valueTB.Text));
                //更改两个账户的余额
                xmlfile.Element("Rest").Value = source.ToString();
                xmlfile.Save("MoneyBox.xml");
                PosPrinter.POSPrinter printer = new PosPrinter.POSPrinter();
                printer.OpenBox();
                this.Close();
                return;
            }
            
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            XElement xmlfile=XElement.Load("MoneyBox.xml");
            this.Title = "给取钱款——当前前项余额：" + xmlfile.Element("Rest").Value;
        }
    }
}
